package com.dqhl.community.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Constant;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by guan on 2016/9/11.
 * 更换昵称 activity
 */
public class UpdataNickNameActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_top_back;
    private TextView tv_top_title;
    private TextView tv_confirm;
    private EditText et_personInfo;
    String newNickName = "";
    private String strNickname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updata_nick_name);
        initView();
    }

    private void initView() {
        strNickname = getIntent().getStringExtra("nickname");

        iv_top_back = (ImageView) findViewById(R.id.iv_top_back);
        iv_top_back.setVisibility(View.VISIBLE);
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("修改昵称");
        tv_confirm = (TextView) findViewById(R.id.tv_confirm);
        tv_confirm.setOnClickListener(this);
        et_personInfo = (EditText) findViewById(R.id.et_personInfo);
        et_personInfo.setText(strNickname);
        et_personInfo.setSelection(strNickname.length()); //设置光标在字的末尾

        iv_top_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.tv_confirm:
                updataNickName();
                break;
        }
    }

    //更换昵称
    public void updataNickName() {
        showCustomLoadBar(R.string.loading);
        newNickName = et_personInfo.getText().toString();
        if ("".equals(newNickName)) {
            hideCustomLoadBar();
            finish();
        } else {
            Dlog.e(newNickName + "GG修改昵称返回" + spUserInfo.getString(Constant.USER_PHONE, ""));
            RequestParams params = new RequestParams(Config.Api.modifyNickname);
            params.addBodyParameter("userid", app.getUserId());
            params.addBodyParameter("nickname_new", newNickName);
            params.addBodyParameter("nickname_old", spUserInfo.getString(Constant.USER_PHONE, ""));
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Dlog.e("GG修改昵称返回" + result);
                    int errCode = JsonUtils.getErrCode(result);
                    String errMsg = JsonUtils.getInfo(result, "errMsg");
                    if (errCode == 0) {
                        SharedPreferences.Editor editor = spUserInfo.edit();
                        editor.putString(Constant.NICKNAME, newNickName);
                        editor.apply();
                        sendUpdateSuccessBroadCast();
                        toast("修改成功");
                        finish();
                    } else {
                        toast("修改失败");
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {
                    hideCustomLoadBar();
                }
            });
        }
    }

    /**
     * 发送一个广播
     */
    private void sendUpdateSuccessBroadCast() {
        Intent intent = new Intent(Constant.ACTION_UPDATA_SUCCESS);
        intent.putExtra(Constant.NICKNAME, newNickName);
        sendBroadcast(intent);
    }
}
